library(archivist)
library(knitr)
library(ggplot2)
# store everything in the archivist repo
addHooksToPrint(class=c("ggplot", "knitr_kable"),
repoDir = "arepo",
repo = "Eseje", user = "pbiecek", subdir = "arepo")
library(scales)
library(dplyr)
library(tidyr)
library(gridExtra)
library(rworldmap)
library(ggthemes)
library(latticeExtra)
library(lattice)
q3 <- function(x) {
a <- quantile(x, c(0.25,0.5,0.75))
names(a) <- c("ymin", "y", "ymax")
a
}
opts_chunk$set(comment=NA, fig.width=6, fig.height=6, results='asis', warning=FALSE, message=FALSE, cache=FALSE)
Here we are using the SmarterPoland package and two datasets.
library(SmarterPoland)
print(kable(head(countries)))
Load: archivist::aread('pbiecek/Eseje/arepo/a0f2357b814a76a46f2a42e831ea5296')
| country | birth.rate | death.rate | population | continent |
|---|---|---|---|---|
| Afghanistan | 34.1 | 7.7 | 30552 | Asia |
| Albania | 12.9 | 9.4 | 3173 | Europe |
| Algeria | 24.3 | 5.7 | 39208 | Africa |
| Andorra | 8.9 | 8.4 | 79 | Europe |
| Angola | 44.1 | 13.9 | 21472 | Africa |
| Antigua and Barbuda | 16.5 | 6.8 | 90 | Americas |
print(kable(head(maturaExam)))
Load: archivist::aread('pbiecek/Eseje/arepo/9797e887eb6f35519246f51946388462')
| id_ucznia | punkty | przedmiot | rok |
|---|---|---|---|
| 4 | 14 | matematyka | 2011 |
| 4 | 31 | j. polski | 2011 |
| 5 | 19 | matematyka | 2010 |
| 5 | 35 | j. polski | 2010 |
| 7 | 16 | matematyka | 2010 |
| 7 | 43 | j. polski | 2010 |
countriesMin <- countries %>%
group_by(continent) %>%
filter(birth.rate == min(birth.rate, na.rm=TRUE))
countriesMax <- countries %>%
group_by(continent) %>%
filter(birth.rate == max(birth.rate, na.rm=TRUE))
theme_ggplain <- theme_bw() + theme(panel.grid.major.x = element_line(color="white"), axis.ticks=element_line(size=0), axis.text=element_text(size=0))
# foirst example
countries$continent <- reorder(countries$continent, countries$birth.rate, median, na.rm=TRUE)
ggplot(countries, aes(x=continent, y=birth.rate, label=country)) +
geom_violin(scale="width", aes(fill=continent), color="white", alpha=0.4) +
stat_summary(fun.data = "q3", geom = "crossbar",
colour = "red", width = 0.5) +
geom_jitter(aes(size=(population)^0.9),position=position_jitter(width = .45, height = 0),
shape=15) +
geom_rug(sides = "l") +
geom_text(data=countriesMin, vjust=2, color="blue3") +
geom_text(data=countriesMax, vjust=-1, color="blue3") +
theme_bw() + xlab("") + theme(legend.position="none", panel.grid.major.x = element_line(color="white"))
Load: archivist::aread('pbiecek/Eseje/arepo/24ea7c04b861083d4bf56eee1c5a17b7')
ggplot(countries, aes(x=continent, y=birth.rate, label=country)) +
geom_jitter(color="white",size=0) +
geom_text(data=countriesMin, vjust=2, color="blue3") +
geom_text(data=countriesMax, vjust=-1, color="blue3") +
theme_bw() + xlab("") + ylab("") + theme_ggplain
Load: archivist::aread('pbiecek/Eseje/arepo/fb548661c44e22f2027cec7e73ecc300')
ggplot(countries, aes(x=continent, y=birth.rate, label=country)) +
geom_violin(scale="width", aes(fill=continent), color="white", alpha=0.4) +
geom_text(data=countriesMin, vjust=2, color="white") +
geom_text(data=countriesMax, vjust=-1, color="white") +
theme_ggplain + xlab("") + ylab("") +theme(legend.position="none")
Load: archivist::aread('pbiecek/Eseje/arepo/68707b3bb377d502f99c72d89d7d76db')
ggplot(countries, aes(x=continent, y=birth.rate, label=country)) +
stat_summary(fun.data = "q3", geom = "crossbar",
colour = "red", width = 0.5) +
geom_text(data=countriesMin, vjust=2, color="white") +
geom_text(data=countriesMax, vjust=-1, color="white") +
theme_ggplain + xlab("") + ylab("")
Load: archivist::aread('pbiecek/Eseje/arepo/2307911ba650efb3f02096641d60510b')
ggplot(countries, aes(x=continent, y=birth.rate, label=country)) +
geom_jitter(aes(size=(population)^0.9),position=position_jitter(width = .45, height = 0),
shape=15) +
geom_text(data=countriesMin, vjust=2, color="white") +
geom_text(data=countriesMax, vjust=-1, color="white") +
theme_ggplain + xlab("") + ylab("") + theme(legend.position="none")
Load: archivist::aread('pbiecek/Eseje/arepo/94ab4e810e6a6c5001c90feea5ebeeef')
ggplot(countries, aes(x=continent, y=birth.rate, label=country)) +
geom_rug(sides = "l") +
geom_text(data=countriesMin, vjust=2, color="white") +
geom_text(data=countriesMax, vjust=-1, color="white") +
theme_ggplain + xlab("") + ylab("")
Load: archivist::aread('pbiecek/Eseje/arepo/e746ca5042fb575da8c4dd62e3b32e2d')
# second example
ggplot(countries, aes(x=birth.rate, y=death.rate)) +
geom_density_2d(h=c(10,10), color="grey") +
geom_point()+
geom_abline(intercept=0,slope=1) +
geom_point(data=countries[132,], color="red", size=4) +
theme_bw() + xlim(0,50)+ylim(-10,20)+ coord_fixed(xlim=c(0,50),ylim=c(0,18), expand = FALSE)
Load: archivist::aread('pbiecek/Eseje/arepo/0283615e0673930bf27c8c83e314c45f')
ggplot(countries, aes(x=birth.rate, y=death.rate)) +
geom_point() + coord_fixed() +
theme_ggplain + xlab("") + ylab("") + xlim(0,50)+ylim(-10,20)+ coord_fixed(xlim=c(0,50),ylim=c(0,18), expand = FALSE)
Load: archivist::aread('pbiecek/Eseje/arepo/19bd87f60510bc98e600125ba4bc1867')
ggplot(countries, aes(x=birth.rate, y=death.rate)) +
geom_point(color="white") + coord_fixed() +
geom_density_2d(h=c(10,10), color="grey") +
theme_ggplain + xlab("") + ylab("") + xlim(0,50)+ylim(-10,20)+ coord_fixed(xlim=c(0,50),ylim=c(0,18), expand = FALSE)
Load: archivist::aread('pbiecek/Eseje/arepo/9473ba871e5a4c7144ca4267c55c510e')
ggplot(countries, aes(x=birth.rate, y=death.rate)) +
geom_point(color="white") + coord_fixed() +
geom_abline(intercept=0,slope=1) +
theme_ggplain + xlab("") + ylab("") + xlim(0,50)+ylim(-10,20)+ coord_fixed(xlim=c(0,50),ylim=c(0,18), expand = FALSE)
Load: archivist::aread('pbiecek/Eseje/arepo/4743712d471db2665da7decbd184654f')
ggplot(countries, aes(x=birth.rate, y=death.rate)) +
geom_point(color="white") + coord_fixed() +
geom_point(data=countries[132,], color="red", size=4) +
theme_ggplain + xlab("") + ylab("") + xlim(0,50)+ylim(-10,20)+ coord_fixed(xlim=c(0,50),ylim=c(0,18), expand = FALSE)
Load: archivist::aread('pbiecek/Eseje/arepo/7b498e17be298db2b1490c5350561303')
ggplot(countries, aes(x=birth.rate, y=death.rate)) +
geom_point() + coord_fixed() +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/1804fce4f6f2646768ea16466ce915a3')
ggplot(countries, aes(x=birth.rate, y=death.rate,
color=continent, shape=continent)) +
geom_point() + coord_fixed() +
theme_bw() + theme(legend.position="top")
Load: archivist::aread('pbiecek/Eseje/arepo/3e8a1a89c861db733c378380bbb98713')
countries$populationCat <- cut(countries$population,
c(1, 10^3, 10^4, 10^5, 10^6, 10^7),
labels = c("< 1M", "< 10M","< 100 M", "< 1 B", "> 1 B"), ordered_result = TRUE)
ggplot(countries, aes(x=birth.rate, y=death.rate,
color=continent, shape=continent,
size=populationCat)) +
geom_point() + coord_fixed() +
theme_bw() + theme(legend.position="top")
Load: archivist::aread('pbiecek/Eseje/arepo/7623f3b17dec60c9de97e12d33796a58')
ggplot(countries, aes(x=birth.rate, y=death.rate,
color=populationCat, shape=continent,
size=populationCat)) +
geom_point() + coord_fixed() + scale_color_brewer(type="seq") +
scale_size_manual(values=c(1,2,3,6,9))+
theme_bw() + theme(legend.position="top")
Load: archivist::aread('pbiecek/Eseje/arepo/a0052657af62cc4209357762f1625a72')
# dotplot
ggplot(countries, aes(x = continent, y = birth.rate)) +
geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 0.7) +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/1d0f90d6fe5aec75eadc4b348dfcf7fb')
# dotplot
ggplot(countries, aes(x = birth.rate, y =death.rate)) +
geom_point() +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/4a91c18f191eb574d4592570ec73a38e')
# jitter
ggplot(countries, aes(x = continent, y =birth.rate)) +
geom_jitter(position = position_jitter(width = .2)) +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/bfb783a2d6c9961f886c1553c4b8ccb4')
# różne mapownia
ggplot() +
geom_point(data=countries, aes(x = birth.rate, y =death.rate, shape=continent), size=4) +
theme_bw() +
scale_shape_manual(values=c("F","A","S","E","O")) +
theme(legend.position=c(0.9,0.17))
Load: archivist::aread('pbiecek/Eseje/arepo/e66d863d39259c97150293fb73574ceb')
ggplot() +
geom_point(data=countries, aes(x = birth.rate, y =death.rate, shape=continent, color=continent), size=4) +
theme_bw() +
scale_shape_manual(values=c("F","A","S","E","O")) +
theme(legend.position=c(0.9,0.17))
Load: archivist::aread('pbiecek/Eseje/arepo/63baa533019560ce8d127cb2c6d24700')
ggplot() +
geom_point(data=countries, aes(x = birth.rate, y =death.rate, color=continent), size=4, shape=19) +
theme_bw() + scale_color_brewer(type = "qual", palette=6) +
theme(legend.position=c(0.9,0.17))
Load: archivist::aread('pbiecek/Eseje/arepo/b3e14462706c5e43f9bbcda3d1a1b351')
ggplot() +
geom_point(data=countries, aes(x = birth.rate, y =death.rate, size=population)) +
scale_size_continuous(trans="sqrt", label=comma, limits=c(0,1500000)) +
theme_bw() + theme(legend.position="none")
Load: archivist::aread('pbiecek/Eseje/arepo/232a27e639574c684a066c24257655f4')
# density
ggplot(countries, aes(x = birth.rate, fill = continent)) +
geom_density(alpha=0.5)+ scale_fill_brewer(type = "qual", palette=6) +
theme_bw() + theme(legend.position=c(0.85,0.85))
Load: archivist::aread('pbiecek/Eseje/arepo/56f275593053d6e89c6a1b5481a1ad4f')
ggplot(countries, aes(x = birth.rate, fill = continent)) +
geom_density( position="fill", color=NA)+ scale_fill_brewer(type = "qual", palette=6) +
theme_bw() + theme(legend.position="none")
Load: archivist::aread('pbiecek/Eseje/arepo/86b199258e8e934b10665ca86b7adda6')
# vioplot
ggplot(countries, aes(x = continent, y = birth.rate, color=continent,fill=continent)) +
geom_violin(scale = "width")+ scale_color_brewer(type = "qual", palette=6)+ scale_fill_brewer(type = "qual", palette=6) +
theme_bw() + theme(legend.position="none")
Load: archivist::aread('pbiecek/Eseje/arepo/34421614fa172412d7af9d53bcfa9a04')
# ribbon
ndf <- countries %>%
group_by(continent) %>%
summarise(birth.rate = weighted.mean(birth.rate, population, na.rm=TRUE),
death.rate = weighted.mean(death.rate, population, na.rm=TRUE))
ggplot() +
geom_ribbon(data=ndf, aes(x=continent, ymax=birth.rate, y=birth.rate, ymin=0, group=1), fill="green3") +
geom_ribbon(data=ndf, aes(x=continent, ymax=death.rate, y=death.rate, ymin=0, group=1), fill="red3") +
theme_bw() + xlab("") + ylab("birth.rate / death.rate")
Load: archivist::aread('pbiecek/Eseje/arepo/b0c631e3e672a85e2a33c975c2773a10')
continents <-
countries %>%
group_by(continent) %>%
summarise(birth.rate = weighted.mean(birth.rate, w = population, na.rm=TRUE),
death.rate = weighted.mean(death.rate, w = population, na.rm=TRUE),
population = sum(population))
# hist
ggplot(countries, aes(x = birth.rate, fill=continent)) +
geom_histogram(binwidth = 2.5) + scale_fill_brewer(type = "qual", palette = 6) +
theme_bw() + theme(legend.position="none")
Load: archivist::aread('pbiecek/Eseje/arepo/6353f46298dae03fcb66e708529e96a2')
# bar
ggplot(countries, aes(x = birth.rate, y = death.rate)) +
geom_rect(xmin=12.38, xmax=27.85, ymin=0, ymax=18, alpha=0.3, fill="grey90") +
geom_point() +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/92cc54cda7e1d2873fc3e1164fcb9c88')
# bar
ggplot(continents, aes(x = continent, y = birth.rate)) +
geom_bar(stat = "identity") +scale_fill_brewer(type = "qual", palette = 6) +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/f58bfc4e97feca1cb360a99ea2cd242c')
continents2 <- continents %>%
mutate(cum = cumsum(population)-population)
ggplot() +
geom_rect(data=continents2, aes(xmin=cum, ymax=0, xmax=cum+population, ymin=birth.rate, fill=continent)) +
geom_text(data=continents2, aes(label=continent, x=cum+population/2, y=birth.rate), vjust=-0.1) +scale_fill_brewer(type = "qual", palette = 6)+
theme_bw() + theme(legend.position="none") +
xlab("population") + ylab("birth.rate")
Load: archivist::aread('pbiecek/Eseje/arepo/047afb44c454a7f147f458f5f59fec71')
ggplot() +
geom_rect(data=continents, aes(xmin = as.numeric(factor(continent))-0.1,
ymin = 0,
xmax = as.numeric(factor(continent))+0.2,
ymax = birth.rate),
fill="green3") +
geom_rect(data=continents, aes(xmin = as.numeric(factor(continent))+0.21,
ymin = birth.rate - death.rate,
xmax = as.numeric(factor(continent))+0.51,
ymax = birth.rate),
fill="red3") +
geom_text(data=continents, aes(x = as.numeric(factor(continent))+0.21,
y = birth.rate + 1,
label = continent)) +
geom_hline(yintercept=0) + ylab("birth.rate - death.rate") + xlab("") +
geom_rug(data=continents, aes(x = as.numeric(factor(continent))+0.21,
y = birth.rate - death.rate), sides="l") +
theme_bw() + theme(axis.text.x = element_text(color="white"),
axis.ticks.x = element_line(color="white"))
Load: archivist::aread('pbiecek/Eseje/arepo/0dd74edded974d6311aadd47aa94157e')
# liczba krajĂłw na kontynent
ggplot(countries, aes(x = continent, fill = continent)) +
geom_bar() +scale_fill_brewer(type = "qual", palette = 6) +
theme_bw() + theme(legend.position="none") + xlab("") + ylab("Liczba krajĂłw")
Load: archivist::aread('pbiecek/Eseje/arepo/e8c2ac889eaf061d08ddc78246418c49')
# slope charts
countries3 <- countries %>%
gather(rate, values, birth.rate, death.rate) %>%
group_by(continent, rate) %>%
summarise(values = mean(values, na.rm=TRUE))
ggplot(countries3, aes(x = rate, y = values, group=continent, color=continent)) +
geom_line(size=2) +
geom_point(size=4) +
theme_bw() + theme(legend.position="none")+scale_color_brewer(type = "qual", palette = 6)
Load: archivist::aread('pbiecek/Eseje/arepo/df3a8ee67cce79e1c5598b310f9a681d')
# line
ggplot(countries, aes(x = birth.rate, y = death.rate)) +
geom_line() +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/fc26c7236495a9eac461d3b88118015f')
# smooth
ggplot(countries, aes(x = birth.rate, y = death.rate)) +
geom_point(color="white", alpha=0) +
geom_smooth(se=FALSE, size=3, color="black") +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/40ab4311481e9cfbd23347628b8fb9fe')
ggplot(countries, aes(x = birth.rate, y = death.rate)) +
geom_point(color="black", alpha=0.3) +
geom_smooth(se=FALSE, size=2, color="red4", method="lm", formula = y~poly(x,1)) +
geom_smooth(se=FALSE, size=2, color="red3", method="lm", formula = y~poly(x,2)) +
geom_smooth(se=FALSE, size=2, color="red1", span=0.5) +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/f62653de2d0d211c78e6a9e2c90b5455')
# arrow
library(grid)
countries$country <- reorder(countries$country, countries$birth.rate, mean)
ggplot() +
geom_segment(data=countries[countries$continent == "Europe",], aes(x = country, xend = country,
y = birth.rate, yend=death.rate),
arrow = arrow(length = unit(0.1,"cm"))) +
theme_bw() + coord_flip() + theme(legend.position="none") +
ylab("<---- more births more deaths ---->") + xlab("")
Load: archivist::aread('pbiecek/Eseje/arepo/e4644a3e0280d4cda662894340b2d433')
ggplot() +
geom_segment(data=countries[countries$continent == "Europe",], aes(x = country, xend = country,
y = birth.rate, yend=death.rate,
color=death.rate > birth.rate),
arrow = arrow(length = unit(0.1,"cm"))) +
theme_bw() + coord_flip() + theme(legend.position="top") +
ylab("<---- more births more deaths ---->") + xlab("") +
scale_color_manual(values = c("green3", "red3"), labels=c("More births than deaths","More deaths than births"), name="")
Load: archivist::aread('pbiecek/Eseje/arepo/9d54087cb037a1f547f6a017c578c320')
ggplot() +
geom_segment(data=countries[countries$continent == "Europe",], aes(x = country, xend = country,
y = birth.rate, yend=death.rate,
size=population),
arrow = arrow(length = unit(0.1,"cm"), type="closed")) +
theme_bw() + coord_flip() + theme(legend.position="none") +
ylab("<---- more births more deaths ---->") + xlab("")
Load: archivist::aread('pbiecek/Eseje/arepo/58e8ea7294ca2250a11c5c1954cd6f6f')
ggplot() +
geom_segment(data=countries[countries$continent == "Europe",], aes(x = country, xend = country,
y = birth.rate, yend=death.rate,
linetype=birth.rate > death.rate),
arrow = arrow(length = unit(0.1,"cm"), type="closed")) +
theme_bw() + coord_flip() + theme(legend.position="top") +
ylab("<---- more births more deaths ---->") + xlab("") +
scale_linetype_manual(values = c(1,2), labels=c("More births than deaths","More deaths than births"), name="")
Load: archivist::aread('pbiecek/Eseje/arepo/0928c675bc99cec25861db9c3b751c57')
# error bars
conts <- countries %>%
group_by(continent) %>%
summarise(bmin = min(birth.rate, na.rm=TRUE),
bmax = max(birth.rate, na.rm=TRUE),
bmea = weighted.mean(birth.rate, w = population, na.rm=TRUE),
dmin = min(death.rate, na.rm=TRUE),
dmax = max(death.rate, na.rm=TRUE),
dmea = weighted.mean(death.rate, w = population, na.rm=TRUE),
population = sum(population, na.rm=TRUE)
)
ggplot(conts, aes(x = bmea, y = dmea,
ymin = dmin, ymax = dmax,
xmin = bmin, xmax = bmax,
color=continent))+
geom_point() +
geom_errorbar(width=0.5) +
# geom_errorbarh(width=0.5) +
theme_bw() + xlab("birth.rate") + ylab("death.rate") +
theme(legend.position="none")
Load: archivist::aread('pbiecek/Eseje/arepo/1e7a3b06202842614fe3cbdac2daf3d7')
# stat_binhex
# geomBinHex.pdf
ggplot(countries, aes(x = birth.rate, y = death.rate)) +
stat_binhex(bins = 9) + scale_fill_gradient(low = "white", high = "black") +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/8fa4f36847f36f15e651f5d36659de7f')
# geomRug
# geomRug.pdf
ggplot(countries, aes(x = birth.rate, y = death.rate)) +
geom_rug() +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/bb502f2c72ec471d662d87c4a3f13985')
continents <-
countries %>%
group_by(continent) %>%
summarise(birth.rate = mean(birth.rate, na.rm=TRUE),
death.rate = mean(death.rate, na.rm=TRUE))
# text
#geomText.pdf
ggplot(continents, aes(x = birth.rate, y = death.rate, label = continent)) +
geom_text(alpha=1) +
theme_bw() + xlim(8,35)
Load: archivist::aread('pbiecek/Eseje/arepo/188dea2ef3d0d29b676c386c11f53cff')
# boxplot
ggplot(countries, aes(x = continent, y = birth.rate)) +
geom_boxplot(fill="grey", coef = 3) +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/dd77d1af09f2fe4e7f768af2bcfe67b5')
# crossbar
ggplot(countries, aes(x = continent, y = birth.rate)) +
geom_jitter(position=position_jitter(width=0.25), color="grey") +
stat_summary(fun.data = "mean_cl_boot", geom = "crossbar", width = 0.3) +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/31dda2c427eccc24022818e9abd338ed')
# strzalki
library(tidyr)
library(grid)
cq <- countries %>%
group_by(continent) %>%
summarise(q1 = quantile(birth.rate, 0.25, na.rm=TRUE),
q2 = quantile(birth.rate, 0.5, na.rm=TRUE),
q3 = quantile(birth.rate, 0.75, na.rm=TRUE))
cq13 <- cq %>%
gather(key, value, -continent)
# statQ1.pdf
ggplot(cq13, aes(x=continent, y=value, group=continent)) +
geom_jitter(data=countries, aes(x = continent, y = birth.rate), position=position_jitter(width=0.25), color="grey") +
geom_path(arrow=arrow(ends = "both"), size=2) +
geom_point(data=cq13[cq13$key == "q2",], aes(x=continent, y=value), size=5) +
theme_bw() + xlab("") + ylab("Kwartyle i mediana dla wsp. urodzin")
Load: archivist::aread('pbiecek/Eseje/arepo/d3fb3e8b20f9a1db1f0ba30129a28523')
# statQ2.pdf
ggplot(cq, aes(x=continent, y=q2)) +
geom_boxplot(aes(ymin=q1, lower=q1, middle=q2, upper=q3, ymax=q3), stat="identity") +
geom_jitter(data=countries, aes(x = continent, y = birth.rate), position=position_jitter(width=0.25), color="grey") +
theme_bw() + xlab("") + ylab("Kwartyle i mediana dla wsp. urodzin")
Load: archivist::aread('pbiecek/Eseje/arepo/5686fa179535372a592b8130bdf0aef1')
# statQ3.pdf
ggplot(cq, aes(x=continent, y=q2)) +
geom_jitter(data=countries, aes(x = continent, y = birth.rate), position=position_jitter(width=0.25), color="grey") +
geom_errorbar(aes(ymin=q1, ymax=q3), stat="identity", width=0.3, size=2) +
geom_text(label="*", size=25) +
theme_bw() + xlab("") + ylab("Kwartyle i mediana dla wsp. urodzin")
Load: archivist::aread('pbiecek/Eseje/arepo/615e90bb747d8a55d39dead899230e3b')
# contour
ggplot(countries, aes(x=birth.rate, y=death.rate)) +
coord_fixed() + xlim(0,50) + ylim(0,16)+
geom_density2d(h=c(10,10), color="grey") +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/e787d850a9599fff218cbe9e4b9527e6')
ggplot(countries, aes(x=birth.rate, y=death.rate)) +
coord_fixed() + xlim(0,50) + ylim(0,16)+
stat_density2d(h=c(10,10), geom="polygon", alpha=0.25) +
theme_bw()
Load: archivist::aread('pbiecek/Eseje/arepo/91dadfe418055a63e5b315603cd71984')
ggplot(countries, aes(x=birth.rate, y=death.rate)) +
coord_fixed() + xlim(0,50) + ylim(0,16)+
stat_density2d(h=c(10,10), geom="tile", aes(fill = ..density..), contour = FALSE) + scale_fill_gradient(low="white", high="black") +
theme_minimal() + theme(legend.position="top")
Load: archivist::aread('pbiecek/Eseje/arepo/5e85dbee52710727c30ee6fb91dbc7e4')
ggplot(countries, aes(x=birth.rate, y=death.rate)) +
coord_fixed() + xlim(0,50) + ylim(0,16)+
stat_density2d(h=c(10,10), n=c(50,15), geom="point", aes(size = ..density..), contour = FALSE) + scale_size_continuous(range=c(0,4)) +
theme_minimal() + theme(legend.position="top")
Load: archivist::aread('pbiecek/Eseje/arepo/e24f1ce5d06ca0b04b2f6010b7d6dc07')
maturaExam2 <- spread(maturaExam, przedmiot, punkty)
tab <- table(matematyka = cut(rank(maturaExam2$matematyka)/nrow(maturaExam2), c(0,0.25,0.5,0.75,1), labels = c("matematyka <25%", "matematyka 25%-50%", "matematyka 50%-75%", "matematyka >75%")),
polski = cut(rank(maturaExam2[,"j. polski"])/nrow(maturaExam2), c(0,0.25,0.5,0.75,1), labels = c("polski <25%", "polski 25%-50%", "polski 50%-75%", "polski >75%")))
df <- data.frame(tab)
dfn <- colnames(df)
ggplot(df, aes_string(x = dfn[1], fill = dfn[2], y = dfn[3])) +
geom_bar(stat = "identity", position = "fill") + theme_bw() +
scale_y_continuous(label = percent) + scale_fill_brewer(type = "div")+
ylab("") + theme(legend.position="none",
axis.text.x = element_text(angle=90, vjust=0.5)) +
ggtitle("position_fill") + xlab("")
Load: archivist::aread('pbiecek/Eseje/arepo/4a9b3f21cabc9389fbf71604bbe1cc9c')
ggplot(df, aes_string(x = dfn[1], fill = dfn[2], y = dfn[3])) +
geom_bar(stat = "identity", position = "stack") + theme_bw() +
scale_fill_brewer(type = "div")+
ylab("") + theme(legend.position="none",
axis.text.x = element_text(angle=90, vjust=0.5)) +
ggtitle("position_stack")+ xlab("")
Load: archivist::aread('pbiecek/Eseje/arepo/6d5397884348e8a8295ec7f4010ec85f')
ggplot(df, aes_string(x = dfn[1], fill = dfn[2], y = dfn[3])) +
geom_bar(stat = "identity", position = "dodge") + theme_bw() +
scale_fill_brewer(type = "div")+
ylab("") + theme(legend.position="none",
axis.text.x = element_text(angle=90, vjust=0.5)) +
ggtitle("position_dodge")+ xlab("")
Load: archivist::aread('pbiecek/Eseje/arepo/644f116c168765ae0c30d64527109f35')
# dotplot
ggplot(countries, aes(x = continent, y = birth.rate)) +
geom_point() +
theme_bw() + #theme(axis.text.x = element_text(angle=90, vjust=0.5)) +
ggtitle("position_identity")+ xlab("")
Load: archivist::aread('pbiecek/Eseje/arepo/5678d2e4fe16700d47bb410ba941dd4e')
ggplot(countries, aes(x = continent, y = birth.rate)) +
geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 0.7) +
theme_bw() + #theme(axis.text.x = element_text(angle=90, vjust=0.5)) +
ggtitle("position_dotplot")+ xlab("")
Load: archivist::aread('pbiecek/Eseje/arepo/0161b827804368b9f62c9caf6935f904')
ggplot(countries, aes(x = continent, y = birth.rate)) +
geom_jitter(position=position_jitter(0.35)) +
theme_bw() + #theme(axis.text.x = element_text(angle=90, vjust=0.5)) +
ggtitle("position_jitter")+ xlab("")
Load: archivist::aread('pbiecek/Eseje/arepo/34df4e336dfccd77f27e06147909bd83')
# dotplot
ddplot <- ggplot(countries, aes(x = birth.rate, y = death.rate)) +
geom_point(size=2) +
geom_smooth(size=2, se=F) +
theme_bw() + theme(legend.position="none", text=element_text(size=20))
ddplot
Load: archivist::aread('pbiecek/Eseje/arepo/f127fdfd575cdf44082bb7e8d90206c8')
ddplot + coord_fixed()
Load: archivist::aread('pbiecek/Eseje/arepo/a107c100410ac299bd82a6a3954d7de9')
ddplot + coord_polar()
Load: archivist::aread('pbiecek/Eseje/arepo/76d33ef977f8843cefa8043d97cb1a21')
ddplot + coord_trans(x = "log2", y = "sqrt")
Load: archivist::aread('pbiecek/Eseje/arepo/bff0840de1ad943fa8bedbdb849674d8')
ddplot + coord_flip()
Load: archivist::aread('pbiecek/Eseje/arepo/60948b00bdf8d1ddb08faa086a4f81f4')
# Mapy
map.world <- map_data(map="world")
countries[177,"country"] = "UK"
countries[135,"country"] = "Russia"
countries[76,"country"] = "Iran"
map2 <- merge(map.world, countries, by.x="region", by.y="country")
map3 <- map2[order(map2$order),]
gg <- ggplot()
gg <- gg + theme(legend.position="none")
gg <- gg + geom_map(data=map3, map=map3, color="grey", aes(map_id=region, x=long, y=lat, fill=birth.rate))
gg <- gg + scale_fill_gradient(low = "yellow", high = "blue4", guide = "colourbar") + theme_bw() + ylim(10,70)+ xlim(-25,50) +
theme(text=element_text(size=20))+theme_map()
gg + coord_map("mollweide")
Load: archivist::aread('pbiecek/Eseje/arepo/9c169b78708e9ce9c72ed194789d727d')
gg + coord_map("ortho", orientation=c(60,20,-10))
Load: archivist::aread('pbiecek/Eseje/arepo/ccda21ef675a17fa36601cb52af27d50')
gg+ coord_map()
Load: archivist::aread('pbiecek/Eseje/arepo/66846763150eb89b10d69d4a97df1892')
# Scale_fill_brewer
df <- data.frame(a=factor(1:8))
pl1 <- list()
pl2 <- list()
pl3 <- list()
for (i in 1:8) {
pl1[[i]] <- ggplot(df, aes(x=a,fill=a)) +
geom_bar() +
scale_fill_brewer(type="qual",palette = i) +
theme(axis.title.x = element_text(size=0, color="black"),
axis.title.y = element_text(size=0, color="black"),
axis.text.y = element_text(size=0, color="black"),
axis.text.x = element_text(size=0, color="black"),
line = element_blank(), rect = element_blank(), #text = element_blank(),
plot.margin = grid::unit(c(0, 0, 0, 0), "lines"), complete = TRUE,
legend.position="none") +
ggtitle(paste("type = qual, palette =",i,""))
pl2[[i]] <- pl1[[i]] + scale_fill_brewer(type="div",palette = i) +
ggtitle(paste("type = div, palette =",i,""))
pl3[[i]] <- pl1[[i]] + scale_fill_brewer(type="seq",palette = i) +
ggtitle(paste("type = seq, palette =",i,""))
}
pl <- c(pl1, pl2, pl3)
pl4 <- sapply(1:8, function(i) list(pl1[[i]], pl2[[i]], pl3[[i]]))
pl$ncol=1
pl$nrow =24
pl4$ncol=3
pl4$nrow =8
pl1$ncol=1
pl1$nrow =8
pl2$ncol=1
pl2$nrow =8
pl3$ncol=1
pl3$nrow =8
do.call(grid.arrange, pl1)
do.call(grid.arrange, pl2)
do.call(grid.arrange, pl3)
do.call(grid.arrange, pl)
do.call(grid.arrange, pl4)
library(ggthemes)
plD <- ggplot(na.omit(countries), aes(x = birth.rate, y = death.rate)) +
geom_point(size=2, color="black") +
theme_bw()
pl <- list()
pl[[1]] <- plD + ggtitle("default")
pl[[2]] <- plD
#pl[[2]] <- plD + scale_x_tufte() + scale_y_tufte() + ggtitle("_tufte")
pl[[3]] <- plD + scale_x_sqrt() + scale_y_sqrt() + ggtitle("_sqrt")
pl[[4]] <- plD + scale_x_reverse() + scale_y_reverse() + ggtitle("_reverse")
pl[[5]] <- plD + scale_x_log10() + scale_y_log10() + ggtitle("_log10")
pl[[6]] <- plD + scale_x_continuous(trans="log2") + scale_y_continuous(trans="log2") + ggtitle("_log2")
sapply(pl, print)
Load: archivist::aread('pbiecek/Eseje/arepo/cbe6119309e1f46739311b334cd805e8') Load:
archivist::aread('pbiecek/Eseje/arepo/a15b4ff6b03e86b82f2efd8a592ff366') Load:
archivist::aread('pbiecek/Eseje/arepo/af4aebd5b57942c3b5c43b4cea103f2a') Load:
archivist::aread('pbiecek/Eseje/arepo/0b7cb9ff662b5fa63d1d0ffe4b3951a4') Load:
archivist::aread('pbiecek/Eseje/arepo/9f158e00d7ed359d584da845418505e5') Load:
archivist::aread('pbiecek/Eseje/arepo/b4326b7d88939ac63300a63dca9a0199') [,1] [,2] [,3] [,4] [,5] [,6]
data List,1 List,1 List,1 List,1 List,1 List,1 panel List,5 List,5 List,5 List,5 List,5 List,5 plot List,9 List,9 List,9 List,9 List,9 List,9
do.call(grid.arrange, pl)
plD <- ggplot(na.omit(countries), aes(x = birth.rate, y = death.rate, shape=continent, color=continent)) +
geom_point(size=5) +
theme_bw()
pl <- list()
pl[[1]] <- plD + ggtitle("default")
pl[[2]] <- plD + scale_shape_stata() + ggtitle("_stata")
pl[[3]] <- plD + scale_shape_calc() + ggtitle("_calc")
pl[[4]] <- plD + scale_shape_tableau() + ggtitle("_tableau")
pl[[5]] <- plD + scale_shape_cleveland() + ggtitle("_cleveland")
pl[[6]] <- plD + scale_shape_manual(values=LETTERS) + ggtitle("_manual")
sapply(pl, print)
Load: archivist::aread('pbiecek/Eseje/arepo/6a9561d306ad219293eb5e29eb28a117') Load:
archivist::aread('pbiecek/Eseje/arepo/29e4fae8aa11e1ca65fce5b65434bcfa') Load:
archivist::aread('pbiecek/Eseje/arepo/e13a78bac3ebdaf16343b8825fc9c805') Load:
archivist::aread('pbiecek/Eseje/arepo/7fb3cb51d58700f8bb5fec0185e63dce') Load:
archivist::aread('pbiecek/Eseje/arepo/42cfb529148f627dee7004d534790450') Load:
archivist::aread('pbiecek/Eseje/arepo/96c98df1e7377fdd30d6440a4c750de8') [,1] [,2] [,3] [,4] [,5] [,6]
data List,1 List,1 List,1 List,1 List,1 List,1 panel List,5 List,5 List,5 List,5 List,5 List,5 plot List,9 List,9 List,9 List,9 List,9 List,9
do.call(grid.arrange, pl)
plD <- ggplot(na.omit(countries), aes(x = birth.rate, y = death.rate)) +
stat_ellipse(color="red4")+
geom_point(data=countries[,-5],size=1,color="grey") +
geom_point(size=2, color="red") +
theme_bw() + theme(legend.position="none") +
theme(text=element_text(size=20)) +
scale_x_sqrt() + scale_y_sqrt()
plD + facet_wrap(~continent)
Load: archivist::aread('pbiecek/Eseje/arepo/a1165fea0c4dbf91b285084d2a115084')
ggplot(na.omit(countries), aes(x = birth.rate, y = death.rate, color=continent)) +
stat_ellipse()+
geom_point(size=2) +
theme_bw() + theme(legend.position="none") +
theme(text=element_text(size=20)) +
scale_x_sqrt() + scale_y_sqrt()
Load: archivist::aread('pbiecek/Eseje/arepo/39cb5b5c1fe372e54eb7724a54388813')
plD <- ggplot(na.omit(countries), aes(x = birth.rate, y = death.rate)) +
geom_point(size=2, color="black") +
scale_x_sqrt()
pl <- list()
pl[[1]] <- plD + theme_bw() + theme(legend.position="none") +
theme(text=element_text(size=20)) + ggtitle("theme_bw")
#pl[[2]] <- plD # + theme_calc() + theme(legend.position="none") +
# theme(text=element_text(size=20)) + ggtitle("theme_calc")
pl[[3]] <- plD + theme_classic() + theme(legend.position="none") +
theme(text=element_text(size=20)) + ggtitle("theme_classic")
#pl[[4]] <- plD + theme_economist() + theme(legend.position="none") +
# theme(text=element_text(size=20, margin = 0)) + ggtitle("theme_economist")
#pl[[5]] <- plD + theme_economist_white() + theme(legend.position="none") +
# theme(text=element_text(size=20)) + ggtitle("theme_economist_white")
pl[[6]] <- plD + theme_excel() + theme(legend.position="none") +
theme(text=element_text(size=20)) + ggtitle("theme_excel")
pl[[7]] <- plD + theme_few() + theme(legend.position="none") +
theme(text=element_text(size=20)) + ggtitle("theme_few")
#pl[[8]] <- plD + theme_fivethirtyeight() + theme(legend.position="none") +
# theme(text=element_text(size=20)) + ggtitle("theme_fivethirtyeight")
#pl[[9]] <- plD + theme_foundation() + theme(legend.position="none") +
# theme(text=element_text(size=20)) + ggtitle("theme_foundation")
pl[[10]] <- plD + theme_gray() + theme(legend.position="none") +
theme(text=element_text(size=20)) + ggtitle("theme_gray")
pl[[11]] <- plD + theme_map() + theme(legend.position="none") +
theme(text=element_text(size=20)) + ggtitle("theme_map")
pl[[12]] <- plD + theme_pander() + theme(legend.position="none") +
theme(text=element_text(size=20)) + ggtitle("theme_pander")
pl[[13]] <- plD + theme_solarized() + theme(legend.position="none") +
theme(text=element_text(size=20)) + ggtitle("theme_solarized")
#pl[[14]] <- plD + theme_stata() + theme(legend.position="none") +
# theme(text=element_text(size=20)) + ggtitle("theme_stata")
#pl[[15]] <- plD + theme_wsj() + theme(legend.position="none") +
# theme(text=element_text(size=20)) + ggtitle("theme_wsj")
pl[[16]] <- plD + theme_tufte() + theme(legend.position="none") +
theme(text=element_text(size=20)) + ggtitle("theme_tufte")
#pl[[17]] <- plD + theme_solarized_2() + theme(legend.position="none") +
# theme(text=element_text(size=20)) + ggtitle("theme_solarized_2")
tmp <- sapply(pl, print)
Load: archivist::aread('pbiecek/Eseje/arepo/ff25ee24ee0017192e87f0be3282504c') NULL Load:
archivist::aread('pbiecek/Eseje/arepo/329e4064745fd002fefe908947c596af') NULL NULL Load:
archivist::aread('pbiecek/Eseje/arepo/6015be582c972c8fc6642adfea43e965') Load:
archivist::aread('pbiecek/Eseje/arepo/c63f6f14dc9638265e7aac64634bfb83') NULL NULL Load:
archivist::aread('pbiecek/Eseje/arepo/111c0620e6b7a00080df7193bcbcea71') Load:
archivist::aread('pbiecek/Eseje/arepo/a1087e4e4edb9bd6624ea4252b48a970') Load:
archivist::aread('pbiecek/Eseje/arepo/d050c431c13e95596a115c3d46781f5a') Load:
archivist::aread('pbiecek/Eseje/arepo/59fcb8d0bc731cf8a908f17901c8efce') NULL NULL Load:
archivist::aread('pbiecek/Eseje/arepo/92be1f8c0d7e3c01108730827444dc82')
#do.call(grid.arrange, pl[c(1,10,11, 8,17,14, 4,5, 6)])